<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="商品ID" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入评论内容关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否隐藏" prop="isHidden">
        <el-select v-model="queryParams.isHidden" placeholder="请选择显示状态" clearable>
          <el-option
            v-for="dict in isHiddenOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['demo:comment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['demo:comment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['demo:comment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['demo:comment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论ID" align="center" prop="commentId" width="80" />
      <el-table-column label="商品ID" align="center" prop="goodsId" width="80" />
      <el-table-column label="用户ID" align="center" prop="userId" width="80" />
      <el-table-column label="评论内容" align="center" prop="content" :show-overflow-tooltip="true" min-width="180" />
      <el-table-column label="评论时间" align="center" prop="commentTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.commentTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否隐藏" align="center" prop="isHidden" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isHidden === '0' ? 'success' : 'info'">
            {{ scope.row.isHidden === '0' ? '显示' : '隐藏' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="点赞数" align="center" prop="likeCount" width="80" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'">
            {{ scope.row.status === '0' ? '正常' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['demo:comment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['demo:comment:remove']"
          >删除</el-button>
          <el-dropdown @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['demo:comment:edit']">
            <span class="el-dropdown-link">
              <i class="el-icon-more"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="toggleHidden">{{ scope.row.isHidden === '0' ? '隐藏评论' : '显示评论' }}</el-dropdown-item>
              <el-dropdown-item command="viewParent" v-if="scope.row.parentId">查看父评论</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品ID" prop="goodsId">
              <el-input v-model="form.goodsId" placeholder="请输入商品ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户ID" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入用户ID" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="父评论ID" prop="parentId">
              <el-input v-model="form.parentId" placeholder="请输入父评论ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评论时间" prop="commentTime">
              <el-date-picker clearable
                v-model="form.commentTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择评论时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="评论内容" prop="content">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否隐藏" prop="isHidden">
              <el-radio-group v-model="form.isHidden">
                <el-radio label="0">显示</el-radio>
                <el-radio label="1">隐藏</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点赞数" prop="likeCount">
              <el-input-number v-model="form.likeCount" :min="0" :step="1" controls-position="right" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio label="0">正常</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看父评论对话框 -->
    <el-dialog title="父评论信息" :visible.sync="parentOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="评论ID">{{ parentComment.commentId }}</el-descriptions-item>
        <el-descriptions-item label="商品ID">{{ parentComment.goodsId }}</el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ parentComment.userId }}</el-descriptions-item>
        <el-descriptions-item label="评论时间">{{ parseTime(parentComment.commentTime) }}</el-descriptions-item>
        <el-descriptions-item label="是否隐藏">
          <el-tag :type="parentComment.isHidden === '0' ? 'success' : 'info'">
            {{ parentComment.isHidden === '0' ? '显示' : '隐藏' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="点赞数">{{ parentComment.likeCount }}</el-descriptions-item>
        <el-descriptions-item label="评论内容" :span="2">
          <div v-html="parentComment.content"></div>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listComment, getComment, delComment, addComment, updateComment } from "@/api/demo/comment";

export default {
  name: "Comment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品评论表格数据
      commentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示父评论弹出层
      parentOpen: false,
      // 父评论对象
      parentComment: {},
      // 日期范围
      dateRange: [],
      // 是否隐藏选项
      isHiddenOptions: [
        {
          dictValue: "0",
          dictLabel: "显示"
        },
        {
          dictValue: "1",
          dictLabel: "隐藏"
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        userId: null,
        parentId: null,
        content: null,
        commentTime: null,
        isHidden: null,
        likeCount: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsId: [
          { required: true, message: "商品ID不能为空", trigger: "blur" },
          { pattern: /^\d+$/, message: "商品ID必须为数字", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" },
          { pattern: /^\d+$/, message: "用户ID必须为数字", trigger: "blur" }
        ],
        content: [
          { required: true, message: "评论内容不能为空", trigger: "blur" }
        ],
        likeCount: [
          { pattern: /^\d+$/, message: "点赞数必须为数字", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品评论列表 */
    getList() {
      this.loading = true;
      listComment(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        commentId: null,
        goodsId: null,
        userId: null,
        parentId: null,
        content: null,
        commentTime: null,
        isHidden: "0",
        likeCount: 0,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commentId);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品评论";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const commentId = row.commentId || this.ids;
      getComment(commentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品评论";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.commentId != null) {
            updateComment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commentIds = row.commentId || this.ids;
      this.$modal.confirm('是否确认删除商品评论编号为"' + commentIds + '"的数据项？').then(function() {
        return delComment(commentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('demo/comment/export', {
        ...this.queryParams
      }, `comment_${new Date().getTime()}.xlsx`)
    },
    /** 处理下拉菜单命令 */
    handleCommand(command, row) {
      if (command === "toggleHidden") {
        // 切换评论隐藏状态
        const comment = { ...row };
        comment.isHidden = comment.isHidden === '0' ? '1' : '0';
        updateComment(comment).then(response => {
          this.$modal.msgSuccess(comment.isHidden === '0' ? "显示成功" : "隐藏成功");
          this.getList();
        });
      } else if (command === "viewParent" && row.parentId) {
        // 查看父评论
        getComment(row.parentId).then(response => {
          this.parentComment = response.data;
          this.parentOpen = true;
        });
      }
    }
  }
};
</script>

<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
  margin-left: 5px;
}
</style>
